package com.t2b.thaicalendar.view;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.t2b.thaicalendar.R;
import com.t2b.thaicalendar.dao.CalendarOfMonthBean;
import com.t2b.thaicalendar.dto.CalendarOfMonthDto;
import com.t2b.thaicalendar.dto.Days;
import com.t2b.thaicalendar.dto.DaysOfWeekDto;
import com.t2b.thaicalendar.dto.WeeksOfMonthDto;

import java.util.Calendar;

/**
 * Created by OLEEB on 6/10/2014.
 */
public class MonthView extends FrameLayout{

    private CalendarOfMonthBean mCalendarBean;
    private CalendarOfMonthDto mCalendarOfMonth;

    private LinearLayout mDayNames;
    private LinearLayout mWeekContainer;
    private ListView mLvHoliday;

    public MonthView(Context context, Calendar calendar) {
        // Getting application context
        super(context);
        mCalendarBean = new CalendarOfMonthBean(context);
        mCalendarOfMonth = mCalendarBean.getCalendar(calendar);

        LayoutInflater inflater = (LayoutInflater)
                getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View content = inflater.inflate(R.layout.month, null, false);
        addView(content);

        mDayNames = (LinearLayout) findViewById(R.id.dayName);
        mWeekContainer = (LinearLayout) findViewById(R.id.week);
        mLvHoliday = (ListView) findViewById(R.id.lvHoliday);

        drawCalendar();
        setHolidayAdapter();
    }

    private void setHolidayAdapter(){
        mLvHoliday.setAdapter(new HolidayAdapter());
    }

    private void drawCalendar() {
        DayNameView dv = new DayNameView(getContext(), mCalendarOfMonth.getDayNames());
        mDayNames.addView(dv, Utils.getVerticalParams());
        mDayNames.setDividerDrawable(getResources().getDrawable(
                android.R.drawable.divider_horizontal_bright));
        mDayNames.setShowDividers(LinearLayout.SHOW_DIVIDER_END);
        mWeekContainer.setDividerDrawable(getResources().getDrawable(
                android.R.drawable.divider_horizontal_bright));
        mWeekContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_END);
        mWeekContainer.setDividerPadding(4);
        WeeksOfMonthDto weeksOfMonthDto = mCalendarOfMonth.getWeeksOfMonthDto();
        for (int week = 0; week < weeksOfMonthDto.getMaximumWeeksOfMonth(); week++) {
            DaysOfWeekDto daysOfWeekDto = weeksOfMonthDto.weeksOfMonth.get(week);
            WeekView weekView = new WeekView(getContext(), daysOfWeekDto);
            mWeekContainer.addView(weekView, Utils.getVerticalParams());
        }
    }

    private class HolidayAdapter extends BaseAdapter{

        private String[] mHolidays;

        public HolidayAdapter(){
            mHolidays = mCalendarBean.getHolidayOfMonth();
        }

        @Override
        public int getCount() {
            return mHolidays.length;
        }

        @Override
        public Object getItem(int i) {
            return mHolidays[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            HolidayViewHolder holder;
            if(view == null){
                LayoutInflater inflater = (LayoutInflater)
                        getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.list_holiday, viewGroup, false);
                holder = new HolidayViewHolder();
                holder.tvHoliday = (TextView) view.findViewById(R.id.textView1);
                view.setTag(holder);
            }else{
                holder = (HolidayViewHolder) view.getTag();
            }
            holder.tvHoliday.setText(mHolidays[i]);
            return view;
        }
    }

    public static class HolidayViewHolder{
        public TextView tvHoliday;
    }
}
